//local Run Url
//http://p1392:8080/ce-static-testedge/html/aypplan/indexAYP.html

var sectionTitle;
var aypTitle;
var sectionId;
var aypId;
var baseOperationUrl;

var isHelpActive = false;

/* BO EDIT MODE */
/**
 * isEditMode: will decide if it is in Edit Mode or View Mode.
 */
var isEditMode = false;
var isTeacherEditMode = false;

//From Server
//edit or view
var ServerEditMode = "edit";
//1 or 0;
var ServerPlanStatus = 1;
/* EO EDIT MODE */

/**
 * TODO: Get the dates from Server
 */
var aypStartDate = new Date(2013, 3, 3);
var aypEndDate = new Date(2014, 4, 4);

var tab1Activated = false;
var tab2Activated = false;
var tab3Activated = false;
var tab4Activated = false;
var tab5Activated = false;
var tab6Activated = false;
var selectedTabID = "";

var tab2ShiftViewModel;
var tab3AssessmentViewModel;
var tab4SubjectViewModel;
var tab5ClassRoomViewModel;

var isLocal = false;
if ($('#startDate').val()) {

} else {

	isLocal = true;
	//Local
	//sectionId = "sect-1c1fffe6-4806-4b66-8081-9ba9e8667fed";

	/**
	 * New Section ID for R&D
	 * Terms can be deleted here. "sect-3a8cac52-a310-44f7-a677-69517545074d";
	 */
	//sectionId = "sect-3a8cac52-a310-44f7-a677-69517545074d";
	sectionId = "sect-bfaaa063-c259-484f-a8c6-5619639f35d5";

	sectionTitle = "from local";
	baseOperationUrl = "http://p1392:8080/group/hinpubsc-abd/academic-year?p_p_id=AYPManagement_WAR_ceportaltestedge&p_p_lifecycle=0&p_p_state=exclusive&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_AYPManagement_WAR_ceportaltestedge_Operation=";
}

function initializeVars() {
	if (this.$) {
		
		infuser.defaults.templateUrl = "/ce-static-testedge/common/html/templates/ayp_academicyearplanner";
		
		/* BO date controller vars */
		displayEvents = true;
		displayHoliday = true;
		displayAttendance = false;
		displayPendingAttendance = false;
		displaySubAssesment = false;
		displayNonWorkingDays = false;
		displayDailyLog = false;
		/* EO date controller vars */
		baseOperationUrl = $('#baseUrl').val();
		sectionId = $('#sectionId').val();
		aypId = $('#aypId').val();
		sectionTitle = $('#sectionTitle').val();
		aypTitle = $('#aypTitle').val();

		ServerEditMode = $('#mode').val();
		ServerPlanStatus = $('#planStatus').val();

		console.log("ServerEditMode :: ", ServerEditMode);
		console.log("ServerPlanStatus :: ", ServerPlanStatus);
		/* For Date Controller */
		date_aypId = aypId;
		console.log("aypId :: ", aypId);
		console.log("date_aypId :: ", date_aypId);

		updateAllDates();

		if (ServerEditMode == "edit") {

			if (ServerPlanStatus == 0) {
				//all edit
				isEditMode = true;
			}
			//only teachers is true for all edit and only teachers // apllied on Tab5
			isTeacherEditMode = true;

		} else {
			isEditMode = false;
		};

		if ($('#startDate').val()) {
			console.log(" from server $('#startDate').val() :: ", $('#startDate').val());
			console.log(" from server $('#endDate').val() :: ", $('#endDate').val());
			aypStartDate = $('#startDate').val();
			aypEndDate = $('#endDate').val();

			var startMonth = Number(aypStartDate.substr(5, 2)) - 1;
			var endMonth = Number(aypEndDate.substr(5, 2)) - 1;

			aypStartDate = new Date(aypStartDate.substr(0, 4), startMonth, aypStartDate.substr(8, 2));
			aypEndDate = new Date(aypEndDate.substr(0, 4), endMonth, aypEndDate.substr(8, 2));
		}

		baseOperationUrl = baseOperationUrl.replace('DUMMY', '').trim();

	} else {
		//Local
		//sectionId = "sect-1c1fffe6-4806-4b66-8081-9ba9e8667fed";

		/**
		 * New Section ID for R&D
		 * Terms can be deleted here. "sect-3a8cac52-a310-44f7-a677-69517545074d";
		 */
		//sectionId = "sect-3a8cac52-a310-44f7-a677-69517545074d";
		sectionId = "sect-bfaaa063-c259-484f-a8c6-5619639f35d5";

		sectionTitle = "from local";
		baseOperationUrl = "http://p1392:8080/group/hinpubsc-abd/academic-year?p_p_id=AYPManagement_WAR_ceportaltestedge&p_p_lifecycle=0&p_p_state=exclusive&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_AYPManagement_WAR_ceportaltestedge_Operation=";
	}

	console.log("aypStartDate :: ", aypStartDate);
	console.log("aypEndDate :: ", aypEndDate);
	console.log("sectionId :: ", sectionId);
	console.log("sectionTitle :: ", sectionTitle);
	console.log("baseOperationUrl :: ", baseOperationUrl);
};

head(function() {

	//$(document).ready(function() {
	if (!isLocal) {
		initializeVars();
	}

	//console.log("head : ", head);
	tab2ShiftViewModel = new Tab2ViewModel();
	tab3AssessmentViewModel = new myNewFrameworkViewModel();
	tab4SubjectViewModel = new myMainSubjectViewModel;

	var myHeaderVM = new headerVM();
	$('#AYPmainTAB').tabs({
		activate : function(e, ui) {
			var key = $(ui.newPanel).prop('id');
			console.log("AYPmainTAB key :: ", key)

			if (key == "tabs-1" && !tab1Activated) {
				console.log("Tab 1 called");
			}

			if (key == "tabs-2") {
				if (!tab2Activated) {
					ko.applyBindings(tab2ShiftViewModel, document.getElementById('tabs-2'));
					tab2Activated = true;
				}
				tab2ShiftViewModel.init()
			}

			if (key == "tabs-3") {

				if (!tab3Activated) {
					ko.applyBindings(tab3AssessmentViewModel, document.getElementById('tabs-3'));

					tab3Activated = true;
				}

				tab3AssessmentViewModel.init()

			}

			if (key == "tabs-4") {
				if (!tab4Activated) {
					ko.applyBindings(tab4SubjectViewModel, document.getElementById('tabs-4'));
					tab4Activated = true;
				}
				tab4SubjectViewModel.init()
			}

			if (key == "tabs-5") {
				if (!tab5Activated) {
					tab5ClassRoomViewModel = new myMainTab5ViewModel()

					ko.applyBindings(tab5ClassRoomViewModel, document.getElementById('tabs-5'));
					tab5Activated = true;
				}

				/**
				 * Initialize the Model again as the data may have changed in Shift.
				 *
				 */
				tab5ClassRoomViewModel.init()
			}
			
			if (key == "tabs-6") {
				if (!tab6Activated) {
					tab6SharedRoomViewModel = new SharedRoomViewModel()

					ko.applyBindings(tab6SharedRoomViewModel, document.getElementById('tabs-6'));
					tab6Activated = true;
				}

				/**
				 * Initialize the Model again as the data may have changed in Shift.
				 *
				 */
				tab6SharedRoomViewModel.init()
			}
			

		}
	});
	$('#tabAYP1').tabs({
		activate : function(e, ui) {
			var key = $(ui.newPanel).prop('id');
			console.log("tabAYP1 key :: ", key);
			selectedTabID = key;
			tab4SubjectViewModel.selectedTabID(selectedTabID);

		}
	});

	// BO 2 Dialogs initiated for Tab4
	$("#dialog-SaveErrorCoSubjects").dialog({
		autoOpen : false,
		width : 420,
		modal : true
	});

	$("#dialog-JsonIOSaveErrorSubjects").dialog({
		autoOpen : false,
		width : 420,
		modal : true
	});
	$("#dialog-SaveErrorSubjects").dialog({
		autoOpen : false,
		width : 420,
		modal : true
	});

	$("#dialog-SaveSubjects1").dialog({
		autoOpen : false,
		width : 420,
		modal : true
	});

	$("#dialog-SaveSubjects2").dialog({
		autoOpen : false,
		width : 420,
		modal : true
	});
	$("#dialog-SaveSubjects3").dialog({
		autoOpen : false,
		width : 420,
		modal : true
	});
	$("#dialog-SaveSubjects4").dialog({
		autoOpen : false,
		width : 420,
		modal : true
	});

	$("#dialog-SaveSubjects5").dialog({
		autoOpen : false,
		width : 420,
		modal : true
	});

	$("#dialog-SaveSubjects6").dialog({
		autoOpen : false,
		width : 420,
		modal : true
	});
	/* 	Loading data on Default START */
	scheduleVM = new scheduleViewModel();
	ko.applyBindings(scheduleVM, document.getElementById("tabs-1"));
	//for title of Section
	ko.applyBindings(myHeaderVM, document.getElementById("sectionTitleDiv"));

	tab1Activated = true;
	$("#add-Schedule").dialog({
		autoOpen : false,
		width : 650,
		modal : true
	});
	$("#dialog-confirmAssessmentDelete").dialog({
		autoOpen : false,
		width : 420,
		modal : true
	});
	$("#dialog-confirmSubjectDelete").dialog({
		autoOpen : false,
		width : 420,
		modal : true
	});

	$("#dialog-confirmScheduleDelete").dialog({
		autoOpen : false,
		width : 420,
		modal : true
	});
	fetchSchedules(sectionId);

	/* 	Loading data on Default END */

	// $("#create-Shift").dialog({
	// autoOpen : false,
	// width : 600,
	// height : 500,
	// modal : true
	// });

	// EO 2 Dialogs initiated for Tab4
	/**
	 * Initialize the tab by Code
	 */
	$('#AYPmainTAB').tabs("option", "active", 0);

	/**
	 * If navigated from "edit" link for teachers.
	 * Then activate the Teachers Tab.
	 */
	if (ServerEditMode == "edit" && ServerPlanStatus == 1) {
		$('#AYPmainTAB').tabs("option", "active", 4);
	};

	/**
	 * For Inner Validation
	 */
	ko.validation.init({
		messagesOnModified : false
	});
	//ko.validation.init({grouping: { deep: true }, messagesOnModified: false });

	//

});

function inputKeyDown(event) {
	// Allow: backspace, delete, tab, escape, and enter
	if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 ||
	// Allow: Ctrl+A
	(event.keyCode == 65 && event.ctrlKey === true) ||
	// Allow: home, end, left, right
	(event.keyCode >= 35 && event.keyCode <= 39)) {
		// let it happen, don't do anything
		return;
	} else {
		// Ensure that it is a number and stop the keypress
		if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
			event.preventDefault();
		}
	}
}

function retrunToAcademicYear(event) {
	console.log("returning to Academic Year ayp_initialcontroller.js");
	location.href = baseOperationUrl.replace("p_p_state=exclusive", "p_p_state=normal") + "getAcademics";
}

function showHelp() {
	console.log("-------> showing help dialog");

	if (!isHelpActive) {
		isHelpActive = true;
		helpType = "aypPlan";
		var active = jQuery("#AYPmainTAB").tabs("option", "active");
		help_scrollPosition = 'aypPlan_section_' + active;

		jQuery(".help").load("/ce-static-testedge/common/html/help.html", function() {
			//scrollTo('ayp_section_'+active);
			console.log('ayp_section_' + active);
		});

	} else {
		isHelpActive = false;
		jQuery(".help").empty();
	}
}

function closeHelpSection() {
	console.log("closing help section");
	isHelpActive = false;
	jQuery(".help").empty();
}